<?php

abstract class Core_Updater_Abstract
{
    protected $_errors   = array();
    protected $_warnings = array();
    protected $_messages = array();

    /**
     * information about the new installation
     *
     * @var array
     */
    protected $_installationInformation = array();

    /**
     * db adapter
     *
     * @var Zend_Db_Table Adapter
     */
    protected $_db;

    /**
     * load the db adapter
     *
     */
    public function __construct($pathToConfig)
    {
        error_reporting(E_ALL | E_STRICT);
        $this->setDbConnection($pathToConfig);
        $this->_db = Zend_Db_Table::getDefaultAdapter();
    }

    public function setDbConnection($pathToConfig)
    {
        $pathToConfig = BASE_PATH . '/' . str_replace('./', '', $pathToConfig);

        if (!file_exists($pathToConfig) || !is_readable($pathToConfig)) {
            throw new Core_Updater_Exception('Config file was not found or is unreadable!');
        }
        $registry = Zend_Registry::getInstance();

        $dbConfig = $registry->get('database')->getConfig();
        $adapter  = (string) $dbConfig['adapter'];

        $dbConfig = array(
            'adapter'  => (string) $dbConfig['adapter'],
            'host'     => (string) $dbConfig['host'],
            'username' => (string) $dbConfig['username'],
            'password' => (string) $dbConfig['password'],
            'dbname'   => (string) $dbConfig['dbname'],
            'prefix'   => (string) $dbConfig['prefix'],
            'charset'  => (string) $dbConfig['charset'],
        );

        $db = Zend_Db::factory($adapter, $dbConfig);
        $db->setFetchMode(Zend_Db::FETCH_OBJ);
        $db->query("SET NAMES 'utf8'");
        $db->query("SET CHARACTER SET 'utf8'");
        Zend_Db_Table::setDefaultAdapter($db);
        return $db;
    }

    public static function getNewVersion()
    {
        return self::VERSION_NEW;
    }

    public static function getOldVersion()
    {
        return self::VERSION_OLD;
    }

    public static function checkVersions($newVersion, $oldVersion)
    {
        if ((string)$newVersion == self::getNewVersion() && (string)$oldVersion == self::getOldVersion()) {
            return true;
        }
        return false;
    }

    public function getInstallationInformation()
    {
        return $this->_installationInformation;
    }

    public function addError($message)
    {
        $this->_errors[] = array('message' => $message);
    }

    public function addWarning($message)
    {
        $this->_warnings[] = array('message' => $message);
    }

    public function addMessage($message)
    {
        $this->_messages[] = array('message' => $message);
    }

    public function getMessages()
    {
        $messages = new stdClass();
        if (count($this->_errors) > 0) {
            $messages->errors = $this->_errors;
        }

        if (count($this->_warnings) > 0) {
            $messages->warnings = $this->_warnings;
        }

        if (count($this->_messages) > 0) {
            $messages->messages = $this->_messages;
        }

        return $messages;
    }

    public function hasErrors()
    {
        if (count($this->_errors) > 0) {
            return true;
        }
        return false;
    }
}